package com.gbx.ximalayating.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.activity.MFTopImgActivity;
import com.gbx.ximalayating.adapter.MonthlyFocusAdapter;
import com.gbx.ximalayating.callback.HttpService;
import com.gbx.ximalayating.entity.ListItemBean;
import com.gbx.ximalayating.entity.MFTotalBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class MonthlyFocusFragment extends Fragment implements Callback<MFTotalBean>, View.OnClickListener, AdapterView.OnItemClickListener {
    private static final String MF_URL = "http://mobile.ximalaya.com/mobile/discovery/v2/" +
            "rankingList/group?channel=and-a1&device=android&includeActivity=true&includeSpecial=true" +
            "&scale=2&version=4.3.98";

    private List<ListItemBean> listProgram, listAnchor;
    private ImageView topImage;
    private ListView mfProgram;
    private MonthlyFocusAdapter programAdapter;
    private String url;
    private int[] footLayoutIds=new int[]{R.id.mf_foot_linear1, R.id.mf_foot_linear2, R.id.mf_foot_linear3, R.id.mf_foot_linear4};
    private int[] footContentLayoutIds=new int[]{R.id.mf_foot_content_linear1, R.id.mf_foot_content_linear2, R.id.mf_foot_content_linear3, R.id.mf_foot_content_linear4};

    //尾视图的linearlayout
    private List<LinearLayout> footLayouts;
    private List<LinearLayout> footContentLayouts;

    public MonthlyFocusFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_monthly_focus, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mfProgram = (ListView) view.findViewById(R.id.mf_main);

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.mf_empty_layout);
        mfProgram.setEmptyView(layout);
        //添加头视图
        View headView = LayoutInflater.from(getContext()).inflate(R.layout.mf_list_headview, null);
        topImage = (ImageView) headView.findViewById(R.id.mf_top_image);
        topImage.setOnClickListener(this);
        //添加尾视图
        View footView = LayoutInflater.from(getContext()).inflate(R.layout.mf_list_footview, null);
        initView(footView);

        mfProgram.addHeaderView(headView);
        mfProgram.addFooterView(footView);

        //初始化数据
        listProgram=new ArrayList<>();
        programAdapter=new MonthlyFocusAdapter(listProgram,getContext());
        mfProgram.setAdapter(programAdapter);
        listAnchor=new ArrayList<>();

        mfProgram.setOnItemClickListener(this);
        //加载数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getContent("and-a1","android",true,true,2,"4.3.98").enqueue(this);
    }
    //初始化尾视图
    private void initView(View view) {
        footLayouts=new ArrayList<>();
        footContentLayouts=new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            footLayouts.add(((LinearLayout) view.findViewById(footLayoutIds[i])));
            footContentLayouts.add(((LinearLayout) view.findViewById(footContentLayoutIds[i])));
        }
    }

    @Override
    public void onResponse(Call<MFTotalBean> call, Response<MFTotalBean> response) {
        MFTotalBean totalBean = response.body();
        String picPath = totalBean.getFocusImages().getList().get(0).getPic();
        Picasso.with(getContext())
                .load(picPath)
                .into(topImage);
        url = totalBean.getFocusImages().getList().get(0).getUrl();

        List<ListItemBean> programList = totalBean.getDatas().get(0).getList();
        List<ListItemBean> anchorList = totalBean.getDatas().get(1).getList();


        listProgram.addAll(programList);
        listAnchor.addAll(anchorList);

        programAdapter.notifyDataSetChanged();

        for (int i = 0; i < listAnchor.size(); i++) {
            Picasso.with(getContext())
                    .load(anchorList.get(i).getCoverPath())
                    .into(((ImageView) footLayouts.get(i).getChildAt(0)));
            String title1 = listAnchor.get(i).getTitle();
            String title2 = listAnchor.get(i).getFirstKResults().get(0).getTitle();
            String title3 = listAnchor.get(i).getFirstKResults().get(1).getTitle();
            List<String> anchorTitles=new ArrayList<>();
            anchorTitles.add(title1);
            anchorTitles.add(title2);
            anchorTitles.add(title3);
            for (int i1 = 0; i1 < footContentLayouts.get(i).getChildCount(); i1++) {
                if (i1>0){
                ((TextView) footContentLayouts.get(i).getChildAt(i1)).setText(i1+" "+anchorTitles.get(i1));
                }else {
                    ((TextView) footContentLayouts.get(i).getChildAt(i1)).setText(anchorTitles.get(i1));
                }
            }
            footLayouts.get(i).setOnClickListener(this);
        }

    }

    @Override
    public void onFailure(Call<MFTotalBean> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mf_top_image:
                Intent intent=new Intent(getActivity(), MFTopImgActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
                break;
            case R.id.mf_foot_linear1:
            case R.id.mf_foot_linear2:
            case R.id.mf_foot_linear3:
            case R.id.mf_foot_linear4:
                Toast.makeText(getContext(), "暂无内容",Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getContext(), "暂无内容",Toast.LENGTH_SHORT).show();
        switch (position) {
            case 1:
//                Intent intent=new Intent(getActivity(), HottestRecommendActivity.class);
//                startActivity(intent);
                break;

        }
    }
}
